/**
 * Created by Administrator on 2017/10/27.
 */


     var id= window.location.href.split("=")[1];
   var price='';
    //alert(id);
    $.ajax({
        url:url+'getFlowersdetails',
        dataType:'json',
        type:'post',
        async:false,
        data:{'id':id},
        success:function(result){
            // console.log(result);
            var status = result.status;
            var code = result.code;
            if(status==1&&code=='1001'){
                var htmls='';
                var data = result.data[0];
                //================加载轮播图 start=============
                var imgs = data['imgs'];
                    for(var i=0;i<imgs.length;i++){
                       htmls +='<a href="javascript:;"><img src="'+imgs[i]+'" ></a>';
                    }
                   $('#swip').html(htmls);
               //================加载轮播图  end================
                //===============加载产品信息数据 start==========

                    $("meta[name='keywords']").attr('content',data['name']);//修改head中的meta
                    $("meta[name='description']").attr('content',data['taocancontent']);
                    $("title").html(data['name']);//设置title中的内容
                if(data['typeid']==4||data['typeid']==3||data['typeid']==6){
                    $('#didian2').css('display','block');
                    var html1='<div class="produt-title">'+data['name']+'</div>'+
                        '<div class="produt-subtitle">'+data['taocancontent']+'</div>'+
                        '<div class="produt-price">￥<span id="prices">'+data['price']+'</span><span class="del-price">价格￥'+data['price_first']+'</span></div>';
                    $('.produt-head').html(html1);//详细页面中的价格
                    var price2 = '￥<span id="prices">'+data['price']+'</span><span class="del-price">价格￥'+data['price_first']+'</span>';
                    $('.price2').html(price2);//弹出框中的价格
                    var title=data['name'];
                    $('.product-toptit').html(title);
                }else{
                    $('#didian1').css('display','block');
                   var html1='<div class="produt-title">'+data['name']+'包月套餐</div>'+
                    '<div class="produt-subtitle">'+data['taocancontent']+'</div>'+
                    '<div class="produt-price">￥<span>'+data['price']+'-'+(data['price']*12).toFixed(2)+'</span><span class="del-price">价格￥'+data['price_first']+'-'+(data['price_first']*12).toFixed(2)+'</span></div>';
                $('.produt-head').html(html1);//详细页面中的价格
                var price2 = '￥<span>'+data['price']+'-'+(data['price']*12).toFixed(2)+'</span><span class="del-price">价格￥'+data['price_first']+'-'+(data['price_first']*12).toFixed(2)+'</span>';
                $('.price2').html(price2);//弹出框中的价格
                var title=data['name']+'包月套餐';
                $('.product-toptit').html(title);
                }


                //price全局变量，用于花卉铲平计算价格
                    price = data['price'];
                //产品详情
                    $('#product-detail').html(data['details']);
                    $('#product-detail p').each(function(){
                        var src=$(this).children('img').attr('src');
                        var k_host = window.location.host;
                       /* if(k_host=='localhost'){//如果是localhost,就用配置好域名
                            k_host =yuming;
                        }*/
                        src=yuming+src;
                        $(this).children('img').attr('src',src);
                    });
                //如果typeid为1，2，3则是花卉种类，4，5，6为非花卉种类
                if(data['typeid']==4||data['typeid']==3||data['typeid']==6){
                      var goodinfo= data['goodinfo'];
                    var goodinfos = goodinfo.split('/');

                    if(goodinfo.indexOf('/')>0&&goodinfos.length>2){


                        var html3='';
                        for(var i=0;i<goodinfos.length;i++){

                            var goods1 ='';
                            var goods2 ='';
                            if(goodinfos[i].indexOf('=')>0){
                                var goods = goodinfos[i].split('=');
                                goods1= goods[0];
                                goods2 =goods[1];
                            }else{
                              var goods = goodinfos[i];
                                goods1=1;
                                goods2=goods;
                            }
                          //  html3 +='<br/><br/>';
                            html3 +='<label><input name="time" type="radio" value="'+goodinfos[i]+'" onclick="checkchoose('+goods1+','+'\'n\''+')"/>'+goods2+'</label>';
                      /*      html3 += '<br/><br/>'+
                                '<label><input name="time" type="radio" value="'+goodinfos[i]+'" />'+goodinfos[i]+'</label>&nbsp;&nbsp;&nbsp;';*/
                          /*  if(i==1||i==3||i==5){
                                html3 +='<br/><br/>';
                            }*/
                        }
                        $('#sever-time').html(html3);
/*                        var html3 = '<br/><br/>'+
                            +'<label><input name="time" type="radio" value="1" onclick="checkchoose(1)"/><script type="text/javascript">echo_month("1个月(4束)");</script> </label>'+'
                        '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input name="time" type="radio" value="3" onclick="checkchoose(3)"/><script type="text/javascript">echo_month("3个月(12束)");</script> </label>'+
                        '<br/><br/><br/><br/>'+
                        '<label ><input name="time" type="radio" value="6" onclick="checkchoose(6)"/><script type="text/javascript">echo_month("6个月(24束)");</script> </label>'+
                        '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input name="time" type="radio" value="12" onclick="checkchoose(12)"/><script type="text/javascript">echo_month("12个月(48束)");</script> </label>';*/
                    }else{
                        html3 =  '<label><input name="time" type="radio" value="'+goodinfo+'" onclick="checkchoose('+1+','+'\'n\''+')"/>'+goodinfo+'</label>&nbsp;&nbsp;&nbsp;';
                        $('#sever-time').html(html3);
                    }
                }

                //===============加载产品信息数据 end============
                //==============加载提货地点信息 start===========
                 var address = result.address;
                 var html4  = '';
                 for(var i=0;i<address.length;i++){
                     html4 +='<option value="'+address[i]['id']+'">'+address[i]['address']+'</option>';
                 }
                 $('#tihuodian').html(html4);
                //==============加载提货地点信息 end=============
            }
        },
        error:function(result) {
            console.log("error")
        }

    });
//=============用户选择花卉产品的时候，选择订购月份后才能计算价格============
    var tlength='';

    function checkchoose(value,type){
        if(type=='f'){
            tlength = value;
        }
           var html='￥<span id="price" attr="'+(value*price).toFixed(2)+'">'+(value*price).toFixed(2)+'</span>';

        $('.price2').html(html);
    }
//===========用户点击订购后，跳转到支付页面===========================
    var price2='';
    var flag='';
    function buy(){
     // alert('你点击了订阅按钮');
       checkinfo();
       if(flag=='n'){
         alert('订阅失败，重新下单');
           return;
        }
        var baoyunames = $('#baoyuename').html();
     
        var prices = $('#price').html();
        var didian1 = $('#didian1').val();
        var didian2 = $('#didian2').val();
        var address='';
        var chooses = $("input[name='time']:checked").val();
        var time1 = $("input[name='time2']:checked").val();
        if(didian1=='block'){
            address = $("#tihuodian").find('option:selected').text();
            if(chooses==''||address==''||typeof(chooses)=='undefined'||time1==''||typeof(time1)=='undefined'){
                alert('请选择后再提交！');
                return;
            }
        }else if(didian2=='block'){
            var adrs = $('#address').val();
            var name = $('#name').val();
            var phones = $('#phones').val();
            var pattern = /^1[34578]\d{9}$/;
            if(pattern.test(phones)){
                alert('请输入正确的手机号！');return;
            }
            address = adrs+'/'+name;
            if(chooses==''||typeof(chooses)=='undefined'||time1==''||typeof(time1)=='undefined'){
                alert('请选择后再提交！');
                return;
            }
        }






      //alert(prices);return;
        discount(prices);
      //  var ii = 'http://pay.yzztnet.com/api/flower/weixin_pay/wxpay?baoyuename='+encodeURI(baoyunames)+'&&price='+fix(price2)+'&&time='+encodeURI(chooses)+'&&address='+encodeURI(address+'/提货时间：'+time1)+'&&tlength='+tlength;
      
        //window.location.href='http://pay.yzztnet.com/api/flower/weixin_pay/wxpay?baoyuename='+encodeURI(baoyunames)+'&&price='+price2.toFixed(2)+'&&time='+encodeURI(chooses)+'&&address='+encodeURI(address+'/提货时间：'+time1)+'&&tlength='+tlength;
        window.location.href=yuming+'/api/flower/weixin_pay/wxpay?baoyuename='+encodeURI(baoyunames)+'&&price='+fix(price2)+'&&time='+encodeURI(chooses)+'&&address='+encodeURI(address+'/提货时间：'+time1)+'&&tlength='+tlength;

    }
function fix(value){
  return Math.floor(value * 100) / 100 ;
}
$('#dingyue').click(function(){
    buy();
});
/**
 * 打折
 */
    function discount(prices){
    $.ajax({
        url:url+'discount',
        dataType:'json',
        type:'post',
        async:false,
        data:{},
        success:function(result){
            // console.log(result);
            var status = result.status;
            var code = result.code;
            if(status==1&&code=='1001'){
                var data = result.userinfo;
                var config = result.config;
                var intergral = data['integral'];
                if(config.length>0){
                    for(var i=0;i<config.length;i++){
                        if(config[i]['key']=='vip2'&&(intergral>config[i]['value'])){
                            var vip2discount = config[7]['value'];
                            price2= prices*(vip2discount/100);
                            return;
                        }else if(config[i]['key']=='vip1'&&((intergral>config[i]['value'])&&(intergral<config[5]['value']))){
                            var vip1discount = config[8]['value'];
                            price2= prices*(vip1discount/100);
                            return;
                        }
                        if(i==(config.length-1)){
                            price2= price;
                        }
                    }
                }else{
                    price2= price;
                    return;
                }


            }
        },
        error:function(result) {
            console.log("error")
        }

    });
    }
/**
 * 检测用户手机号，是否绑定
 */
function checkinfo(){

    $.ajax({
        url:url+'getuserinfo',
        dataType:'json',
        type:'post',
        async:false,
        data:{},
        success:function(result){
            // console.log(result);
            var status = result.status;
            var code = result.code;
            var data = result.data;
       
          if(status==1&&code=='1001'&&data!=null){
                //var usercenter = result.usercenter;
                if(data['phone']==''){
                    alert('请到个人中心绑定您的手机号！');
                    flag='n';
                }
            }else{
                alert('请到个人中心绑定您的手机号！');
                flag='n';
            }
        },
        error:function(result) {
            console.log("error");
            alert('error');
        }

    });
    $('#alerturl').attr('action',yuming+'/api/index.php/flower/weixin_pay/wxpay');
}

